/**
 * Adds an Autocomplete function to the input and gets the
 * suggestions for the module
 * 
 * @param input JQuery reference to the input
 * @param module_id id of the module in this view
 * 
 * @author Thomas Lottermann
 */
function LecturerAutoComplete(input, module_id) {
	var _this = this;
	this.input = input;
	
	var cache = {};

	/**
	 * 
	 * 
	 * @param request Data from JQuery
	 * @param response Needed by JQuery to be called, when the ajax call comes back
	 */
	this.call = function call(request, response) {
		var term = request.term;
		if(term in cache){
			response(cache[term]);
			return;
		} 
		Dajaxice.documents.lecturer_name_suggestions(
			function(data) {
				cache[term] = data.suggestions
				response(data.suggestions);
			},
			{'module_id':module_id, 'lecturer_name':term}
		);
	}
	

	this.init = function init() {
		Dajaxice.setup({'default_exception_callback': function(){}});
		this.input.autocomplete(
			{
				source: this.call,
				minLength: 0
			}
		);
		
		// Activate Autocomplete as soon the input has focus
		this.input.focus(function(){
			_this.input.autocomplete("search", _this.input.val());
		});
	}
	this.init();
}